package com.my.test;

import java.util.ArrayList;
import java.util.List;

import com.my.utils.StrBuilder;
import com.my.utils.StringUtil;

//多线程执行结果统计
public class MultiThreadResult {

	private List<ThreadTask> threadTasklist;
	private long total;
	private long createTotal;//创建线程耗时

	public MultiThreadResult(int count) {
		threadTasklist = new ArrayList(count);
	}

	//增加人物到人物列表
	public void addTask(ThreadTask ThreadTask) {
		threadTasklist.add(ThreadTask);
	}
	
	public void setTotal(long total) {
		this.total = total;
	}

	/**
	 * @param detail 是否显示明细
	 * */
	public String getResult(boolean detail) {
		StrBuilder sb = new StrBuilder();
		
		//所有线程耗时
		long threadTotal = 0;

		if(detail){
			sb.appendLine("--------------------------------");
		}
		for (int i = 0; i < threadTasklist.size(); i++) {
			long con = threadTasklist.get(i).getConsume();
			threadTotal += con;
			if (detail) {
				sb.appendLine(i + ":" + toMs(con) + "(ms)");
			}
		}
		sb.append(threadTasklist.size() +"个线程,创建耗时:"+toMs(createTotal)+"(ms),执行耗时:" + toMs(total) + "(ms),平均耗时" + toMs((threadTotal + 0D) / threadTasklist.size()) + "(ms)");
		return sb.toString();
	}

	public String getResult() {
		return getResult(false);
	}

	private double toMs(double nano) {
		double d = nano / (1000000);
		return StringUtil.round(d, 4);
	}

	public List<ThreadTask> getTaskList() {
		return threadTasklist;
	}

	public void setCreateTotal(long createTotal) {
		this.createTotal = createTotal;
	}

	
}
